import { acceptHMRUpdate, defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: {
      userId: '',
      nickName: '',
      avatar: '',
      token: ''
    }
  }),
  actions: {
    async loginAction(data: any) {
      const res = await http.post('preschool/user/login', data)
      this.userInfo.token = res.token
      const token = useCookie('lin-token')
      token.value = this.userInfo.token
      console.log("token" + token.value);
      this.getUserInfo()
    },
    async getUserInfo() {
      const res = await http.get('user/userInfo')
      this.userInfo.avatar = res.avatar
      this.userInfo.nickName = res.nickName
      this.userInfo.userId = res.userId
      const userInfo = useCookie('userInfo')
      userInfo.value = JSON.stringify(this.userInfo)
    }
  },
  getters: {
    getAvatar: (state) => state.userInfo.avatar,
    getToken: (state) => state.userInfo.token,
    getUserId: (state) => state.userInfo.userId
  }
})